#pragma once
#include <iostream>
#include "reverse_iterator.hpp"

namespace lw
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _prev;
		ListNode<T>* _next;
		T _data;
		ListNode(const T& val = T())
			:_prev(nullptr), _next(nullptr), _data(val)
		{}
		ListNode(T&& val)
			:_prev(nullptr), _next(nullptr), _data(std::move(val))
		{}
	};

	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef ListNode<T> ListNode;
		typedef __list_iterator<T, Ref, Ptr> Self;
		ListNode* _node;
		__list_iterator(ListNode* node)
			:_node(node)
		{}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		bool operator==(const Self& s) const
		{
			return _node == s._node;
		}
		bool operator!=(const Self& s) const
		{
			return _node != s._node;
		}
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &_node->_data;
		}
		operator bool()
		{
			return _node;
		}
	};

	template<class T>
	class list
	{
		typedef ListNode<T> ListNode;
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<const T, const T&, const T*> const_iterator;
		typedef __reverse_iterator <iterator, T&, T*> reverse_iterator;
		typedef __reverse_iterator <const_iterator, const T&, const T*> const_reverse_iterator;

	public:
		iterator begin(){	return _head->_next;	}
		iterator end(){		return _head;	}
		const_iterator begin() const{	return _head->_next;	}
		const_iterator end() const{		return _head;	}
		reverse_iterator rbegin(){	return end();	}
		reverse_iterator rend() { return begin(); }
		const_reverse_iterator crbegin() { return end(); }
		const_reverse_iterator crend() { return begin(); }

		void Init()
		{
			_head = new ListNode();
			_head->_next = _head;
			_head->_prev = _head;
		}
		list()
		{
			Init();
		}
		template<class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			Init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		list(const std::initializer_list<T>& il)
		{
			Init();
			for (const auto& e : il)
				push_back(e);
		}
		list(const list<T>& lt)
		{
			Init();
			for (const auto& e : lt)
				push_back(e);
		}
		list(list<T>&& lt)
		{
			Init();
			swap(lt);
		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		list<T>& operator=(list<T>&& lt)
		{
			swap(lt);
			return *this;
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
		}
		void push_back(const T& val)
		{
			insert(_head, val);
		}
		void push_back(T&& val)
		{
			insert(_head, std::move(val));
		}
		void push_front(const T& val)
		{
			insert(_head->_next, val);
		}
		void push_front(T&& val)
		{
			insert(_head->_next, std::move(val));
		}
		void pop_back()
		{
			erase(_head->_prev);
		}
		void pop_front()
		{
			erase(_head->_next);
		}
		iterator insert(iterator pos, const T& val)
		{
			ListNode* node = pos._node;
			ListNode* prev = node->_prev;
			ListNode* newnode = new ListNode(val);

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = node;
			node->_prev = newnode;

			return newnode;
		}		
		iterator insert(iterator pos, T&& val)
		{
			ListNode* node = pos._node;
			ListNode* prev = node->_prev;
			ListNode* newnode = new ListNode(std::move(val));

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = node;
			node->_prev = newnode;

			return newnode;
		}
		iterator erase(iterator pos)
		{
			ListNode* node = pos._node;
			ListNode* prev = node->_prev, *next = node->_next;
			prev->_next = next;
			next->_prev = prev;
			
			delete node;
			return next;
		}
		void clear()
		{
			ListNode* curr = _head->_next;
			while (curr != _head)
			{
				ListNode* del = curr;
				curr = curr->_next;
				erase(del);
			}
		}

		bool empty() const
		{
			return _head->_next == _head;
		}
	private:
		ListNode* _head = nullptr;
	};
}